package com.group.flagtivities;

import java.io.InputStream;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.methods.HttpGet;

import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.Shader.TileMode;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.net.http.AndroidHttpClient;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.widget.TextView;

public class ImageUtil {
	
	public static BitmapDrawable cropImage(BitmapDrawable d, int size, DisplayMetrics metrics) {
        size = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, size, metrics);
        
        Bitmap b = d.getBitmap();
        Bitmap target = Bitmap.createBitmap(size, size, Config.ARGB_8888);
        Canvas canvas = new Canvas(target);
        
        float hScale = size/(float)b.getWidth();
        float vScale = size/(float)b.getHeight();
        float scale = Math.max(hScale,vScale);
        
        Matrix matrix = new Matrix();
        matrix.setScale(scale, scale);
        matrix.postTranslate(size/2 - b.getWidth()/2 * scale, size/2 - b.getHeight()/2 * scale);
        
        canvas.drawColor(Color.BLACK);
        canvas.drawBitmap(b, matrix, new Paint());
        
        BitmapDrawable bmd = new BitmapDrawable(target);
        bmd.setBounds(0,0,size,size);
        
        return bmd;
	}
	
	public static Bitmap downloadBitmap(String url) {
		final AndroidHttpClient client = AndroidHttpClient.newInstance("Android");
		final HttpGet getRequest = new HttpGet(url);
		
		try {
			HttpResponse response = client.execute(getRequest);
			final int statusCode = response.getStatusLine().getStatusCode();
			if (statusCode != HttpStatus.SC_OK) {
				return null;
			}
			final HttpEntity entity = response.getEntity();
			if (entity != null) {
				InputStream inputStream = null;
				try {
					inputStream = entity.getContent();
					final Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
					return bitmap;
				} finally {
					if (inputStream != null) {
						inputStream.close();
					}
					entity.consumeContent();
				}
			}
		}catch (Exception e) {
			getRequest.abort();
		}finally {
			if (client != null) {
				client.close();
			}
		}
		
		return null;
	}
	
	public static void download(String url, TextView textView, DisplayMetrics metrics) {
		BitmapDownloaderTask task = new BitmapDownloaderTask(textView, metrics);
		task.execute(url);
	}
	
	private static Boolean matchPixelToTransparent(final int pixel) {
		if (pixel == Color.TRANSPARENT) return true;
		return false;
	}
	
	public static Drawable colorImage(Drawable d, int color) {
		Bitmap bm = ((BitmapDrawable) d).getBitmap();
		bm = bm.copy(Bitmap.Config.ARGB_8888, true);
		
		for (int x = 0; x < bm.getWidth(); x++) {
			for (int y = 0; y < bm.getHeight(); y++) {
				if (matchPixelToTransparent(bm.getPixel(x, y))) {
					bm.setPixel(x, y, color);
				}
			}
		}
		
		return new BitmapDrawable(bm);
	}
	
	
	public static Drawable colorFlag(Drawable d, int startColor, int endColor) {
		Bitmap flag = ((BitmapDrawable) d).getBitmap();
		
		Bitmap coloredFlag = Bitmap.createBitmap(flag.getWidth(), flag.getHeight(), Config.ARGB_8888);
		Canvas canvas = new Canvas(coloredFlag);
		canvas.drawColor(Color.BLACK);
		
		LinearGradient gradient = new LinearGradient(0, 0, coloredFlag.getWidth(), coloredFlag.getHeight(), startColor, endColor, TileMode.CLAMP);
		Paint paint = new Paint();
		paint.setStyle(Style.FILL);
		paint.setShader(gradient);
		
		canvas.drawRect(0, 0, coloredFlag.getWidth(), coloredFlag.getHeight(), paint);
		
		for (int x = 0; x < flag.getWidth(); x++) {
			for (int y = 0; y < flag.getHeight(); y++) {
				if (matchPixelToTransparent(flag.getPixel(x, y))) {
					coloredFlag.setPixel(x, y, Color.TRANSPARENT);
				}
			}
		}
		
		return new BitmapDrawable(coloredFlag);
	}
	
	public static Drawable colorSwatch(int width, int height, int startColor, int endColor) {
		Bitmap colorSwatch = Bitmap.createBitmap(width, height, Config.ARGB_8888);
		Canvas canvas = new Canvas(colorSwatch);
		canvas.drawColor(Color.BLACK);
		
		LinearGradient gradient = new LinearGradient(0, 0, colorSwatch.getWidth(), colorSwatch.getHeight(), startColor, endColor, TileMode.CLAMP);
		Paint paint = new Paint();
		paint.setStyle(Style.FILL);
		paint.setShader(gradient);
		
		canvas.drawRect(0, 0, colorSwatch.getWidth(), colorSwatch.getHeight(), paint);
		
		for (int i = 0; i < colorSwatch.getWidth(); i++) {
			colorSwatch.setPixel(0, i, Color.WHITE);
			colorSwatch.setPixel(colorSwatch.getHeight() - 1, i, Color.WHITE);
		}
		
		for (int i = 0; i < colorSwatch.getHeight(); i++) {
			colorSwatch.setPixel(i, 0, Color.WHITE);
			colorSwatch.setPixel(i, colorSwatch.getWidth() - 1, Color.WHITE);
		}
		
		return new BitmapDrawable(colorSwatch);
	}
	
}
